package com.cy.system.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cy.system.dto.RoleDto;
import com.cy.system.entity.Role;
import com.cy.system.entity.RoleDept;
import com.cy.system.entity.RoleMenu;
import com.cy.system.entity.RoleOper;
import com.cy.system.service.RoleService;

@Controller
public class RoleController {
	
	private final static Logger logger = Logger.getLogger(RoleController.class);
	@Resource
	RoleService roleService;
	
	@RequestMapping("/editRole")
	public String editRole(String editFlag,String roleId,ModelMap map) {
		logger.info("==> editRole editFlag["+editFlag+"]"+"roleId["+roleId+"]");
		if(editFlag.equals("2")&&null!=roleId) {
			Role role =roleService.getRoleById(Integer.parseInt(roleId));
			map.put("role", role);
			List<Integer> menuIds = roleService.getMenuIdsByRoleId(Integer.parseInt(roleId));
			List<Integer> deptIds = roleService.getDeptIdsByRoleId(Integer.parseInt(roleId));
			List<Integer> operIds = roleService.getOperIdsByRoleId(Integer.parseInt(roleId));
			map.put("menuIds", menuIds);
			map.put("deptIds", deptIds);	
			map.put("operIds", operIds);	
		}else {
			map.put("menuIds", editFlag);
			map.put("deptIds", editFlag);	
			map.put("operIds", editFlag);
		}
		return "system/role/roleEditPage";
	}
	
	@PostMapping("/editRole")
	@ResponseBody
	@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED)
	public int editRolePost(@RequestBody RoleDto roleDto,HttpSession session) {
		logger.info("==> roleDto["+roleDto+"]");
		Role role = new Role();
		if(roleDto.getRole().getRoleId()==0) {
			role.setRoleName(roleDto.getRole().getRoleName());
			roleService.addRole(role,session);
			if(0 != role.getRoleId()) {
				List<RoleMenu> roleMenuList = roleDto.getRoleMenus();
				for(RoleMenu rm : roleMenuList) {
					rm.setRoleId(role.getRoleId());
				}
				roleService.addRoleMenusBatch(roleMenuList);
				List<RoleDept> roleDeptList = roleDto.getRoleDepts();
				for(RoleDept rd : roleDeptList) {
					rd.setRoleId(role.getRoleId());
				}
				roleService.addRoleDeptsBatch(roleDeptList);
				List<RoleOper> roleOperList = roleDto.getRoleOpers();
				for(RoleOper ro : roleOperList) {
					ro.setRoleId(role.getRoleId());
				}
				roleService.addRoleOpersBatch(roleOperList);
				logger.info("==> editRole roleMenuList["+roleMenuList+"]");	
				return 1;
			} 					
		}else {
			role = roleDto.getRole();
			logger.info("==> 更新role["+role+"]");
			roleService.updateRole(role,session);
			roleService.delRoleMenusById(role.getRoleId());
			roleService.addRoleMenusBatch(roleDto.getRoleMenus());
			roleService.delRoleDeptsById(role.getRoleId());
			roleService.addRoleDeptsBatch(roleDto.getRoleDepts());
			roleService.delRoleOpersById(role.getRoleId());
			roleService.addRoleOpersBatch(roleDto.getRoleOpers());
			logger.info("==> 更新roleMenus["+roleDto.getRoleMenus()+"]");		
			return 1;
		}
		return 1;
	}
	@RequestMapping("/deleteRole")
	@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED)
	public String deleteRole(int roleId) {
		logger.info("==> 删除角色 roleId["+roleId+"]");
		roleService.delRoleById(roleId);
		roleService.delRoleDeptsById(roleId);
		roleService.delRoleMenusById(roleId);
		roleService.delRoleOpersById(roleId);
		
		return "1";
	} 
}	
